<?php require_once 'httpFilter.php'; 'conf.php'; ?>
<html>
<title>Transaction view</title>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/ui-lightness/jquery-ui.css" />
	

<script type="text/javascript"
	src="http://www.google.com/jsapi?key=ABQIAAAAN5ZtdO4QwxMtTu20xDDGaBRDmw7J9fBy0SHOM2DFag_Hy1S5lhQ5rqB1kcjLa1mWGmKBHM2W7xPDyQ">
</script>

<link rel="stylesheet" type="text/css" href="css/mbContainer.css" title="style"  media="screen"/>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
  <script type="text/javascript">
	google.load("jqueryui", "1.8.15");
	</script>
  <script type="text/javascript" src="container/jquery.metadata.js"></script>
  <script type="text/javascript" src="container/mbContainer.js"></script>

<script type="text/javascript" src="js/jquery.datePicker-2.1.2.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<link rel="stylesheet" type="text/css" href="css/datePicker.css" title="style"  media="screen"/>

<script type="text/javascript" src="savingsView.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
</head>
<body>
  <div id="loadingPic" style="display: none; position:absolute; left: 0px; top:0px; height:100%; width: 100%; text-align: center;
	           background-image:url('images/translucent.gif'); z-index: 1000;">
	<img src="images/loading2.gif" style="margin: 12%"/>
	</div>
  <input id="currentSubAccount" type="hidden"/>
  <div id="fundsTransferDiv" style="display: none;">
  		<table  width="400" cellpadding="0" cellspacing="0">
		<tr>
			<td>Transfer from:</td>
			<td><select id="transferFromAccount"></select></td>
		</tr>
		<tr>
			<td>To:</td>
			<td><select id="transferToAccount"></select></td>
		</tr>
		<tr>
			<td>Amount:</td>
			<td>R &nbsp; <input id="transferAmount" type="text" class="shortest"/></select></td>
		</tr>
		</table>
  </div>
  <div id="transactionsView" style="display: none;" class="scrollable">
  		<table  width="500" cellpadding="0" cellspacing="0">
			<tr class="savTransHeaderRow">
				<th>date</th>
				<th>description</th>
				<th>Expense</th>
				<th>Income</th>
			</tr>
		</table>
  </div>
  <div id="demoContainer" style="display: none;" title="Reconcile Transaction">
    <div id="dock"> <input type="hidden" id="currentMainTransaction"/></div>
    <table  width="600" cellpadding="0" cellspacing="0">
		<tr class="transHeaderRow">
			<th>date</th>
			<th>account</th>
			<th>description</th>
			<th>Rc</th>
			<th>Expense</th>
			<th>Income</th>
			<th>Balance</th>
		</tr>
	</table>
	<br/>
	<fieldset class="normalText"><legend>Reconciliation</legend>
		<table width="100%">
			<tr>
				<td width="60%">Amount to Reconcile:</td><td><span id="reconAmount">R 0</span></td>
			</tr>
			<tr>
				<td>Selected Transactions Total:</td><td><span id="selectedAmount">R 0</span></td>
			</tr>
			<tr>
				<td>Difference:</td><td><span id="reconDiffAmount" style="font-size: 1.5em; font-weight: normal;">R 0</td>
			</tr>
		</table>
	</fieldset>
	<span class="normalText">
		<button id="reconBtn" disabled onclick="doReconcile();">Reconcile</button>
		<button onclick="showCreateTransContainer();">Create TX</button>
		<button onclick="createFillerTransaction();">Create Filler TX</button>
		<button onclick="closeTransWindow();">Cancel</button>
	<span>
  </div>
  
  <div id="fileUploadContainer"  style="top:100px; left:30%; display: none;"  title="Statement Upload">
	 	<form id="fileUploadForm" name="fileUploadForm" method="post" enctype="multipart/form-data" action="index.php?mod=uploadSavingsStatement">
	 		<input type="hidden" id="action" name="action" value="uploadStatement"/>
	 		<input type="hidden" id="accId" name="accId"/>
			<table width="450">
				<tr>
					<td>Select File:</td>
					<td><input id="transactionFile" type="file" class="longTextInput" name="transactionFile"/></td>
				</tr>
			</table>
		</form>
	 </div>
	
	<!-- NAVIGATION -->
	<?php include("includes/navigation.php"); ?>
	
		
	<input id="accountId" type="hidden" value="<?=$_GET["accId"]?>"/>
	<br/>
	<?
	require_once "services/budgetServices.php";
	$budgetServices = new BudgetService;

	$userid = $_SESSION["userid"];
	
	$con = mysql_connect(GlobalConfig::db_host_name,GlobalConfig::db_username,GlobalConfig::db_password);
			if (!$con)  {
			  die('Could not connect: ' . mysql_error());
			 }
			
			mysql_select_db(GlobalConfig::db_name, $con);
			$query = "select s.id, s.budgetitemid, b.description, s.openingBalance from subaccount s, budgetitem b where s.budgetitemid=b.id and s.parentAccId=" . $_GET["accId"];
			error_log($query);
			$result = mysql_query($query);
			$count = 0;
			$storedResults = array();
			while ($row = mysql_fetch_array($result)){
				$storedResults[$count] = $row;
				$count++;
			}
	
	?>
	
	<table width="700" border="0" cellpadding="0" cellspacing="0" style="background: #E6EAE9;">
		<tr>
			<td width="100%" style="background: #E6EAE9;" valign="top">
			
			<table width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<th scope="col" colspan="2">
						<a href="#" onclick="toggleCategoryBalances();"><img src="images/add.png" width="12" border="0"></img></a>
						SUB-ACCOUNT BALANCES
					</th>
				</tr>
				<tr>
					<th class="specalt">Account Name <input id="toggleZeroAccountCheckBox" type="checkbox" checked onchange="toggleZeroAccounts();"> 0</th>
					<th class="specalt" width="30%">Balance 
					<a href="#" id="uploadBtn"><img src="images/upload.png" title="Upload a statement" alt="Upload a statement" border="0" width="15"/></a>
					</th>
				</tr>
			<?	
					foreach ($storedResults as $value){
					
						$result1 = mysql_query("select sum(amount) as sum from transaction where budgetitemid=". $value["budgetitemid"] ." and categoryId=4 and accountId=". $_GET["accId"]);
					  	$sum = mysql_fetch_array($result1);
					  	
					  	$itemTotal = round(($value["openingBalance"] + $sum["sum"]), 2);
					  	if ($itemTotal == 0){ 
			?>			
				<tr class="category zeroCat" style="display: none;">
					<td class="spec" width="200"><a href="#" id="<?=$value["budgetitemid"]?>" onclick="updateTransaction(this);"><?=$value["description"]?></a></td>
					<td class="spec" width="80">R <label class="subAccTotal"><?= $itemTotal?></label></td>
				</tr>
						
			<?	  		
					  		
					 } else {
			?>			
				<tr class="category">
					<td class="spec" width="200"><a href="#" id="<?=$value["budgetitemid"]?>" onclick="updateTransaction(this);"><?=$value["description"]?></a></td>
					<td class="spec" width="80">R <label class="subAccTotal"><?=$itemTotal?></label></td>
				</tr>
						
			<?			
					}
			}
				
			?>
			
				<tr>
			    <th scope="row" class="nobg"">&nbsp;</th>
			    <td class="total">R <label id="savingsTotal"></label></td>
			  </tr>
			</table>
		</td>
		</tr>
	</table>
	<br/>
	
	<!--*********************** UNALLOCATED TRANSACTIONS  -->
	<table width="700">
		<tr>
		<td style="background: #E6EAE9;" valign="top">
		<table  width="100%" cellpadding="0" cellspacing="0">
		 	<tr class="headerRow">
				<th width="200" colspan="6">
					<a href="#" onclick="toggleCategoryBalances();"><img src="images/add.png" width="12" border="0"></img></a>
					UNALLOCATED TRANSACTIONS
				</th>
			</tr>
			 	
		 	<tr class="headerRow">
				<th  class="specalt date">date</th>
				<th class="specalt">description</th>
				<th class="specalt rf">Rf</th>
				<th class="specalt expense">Expense</th>
				<th class="specalt income">Income</th>
				<th class="specalt balance">Balance</th>
			</tr>
			<?$result = mysql_query("select * from transaction where accountId = " .$budgetServices->getSavingsAccountId($userid) ." and amount<> 0 and parentTransId=0 and categoryId=6 and reconciled is false order by date asc");
				while ($row = mysql_fetch_array($result)){
			?>
			<tr id="<?=$row["id"]?>" class="unReconTransactions" amount="<?=$row["amount"]?>">
					<td class="transDate"><?=$row["date"]?></td>
					<td class="transDescription" nowrap="true"><a href="#" onclick="showTransactionAllocationDlg();"><?=$row["description"]?></a></td>
					<td><input class="reconCheckbox" type="checkbox" value="<?=$row["reconciled"]?>" disabled/></td>
					<td class="expenseAmount"><? if ($row["amount"] < 0) echo 'R <label class="transmount">'. $row["amount"] * -1; else echo "&nbsp;" ?></label></td>
					<td class="incomeAmount"><? if ($row["amount"] >= 0) echo 'R <label class="transmount">'. $row["amount"]; else echo "&nbsp;" ?></label></td>
					<td>R 0</td>
				</tr>
			<?
			}
								
			?>
			</table>
		
		</td>
	</tr>
</table>
	<br/>
	<!--*********************** UNRECONCILED TRANSACTIONS  -->
	
	<table width="700">
		<tr>
		<td style="background: #E6EAE9;" valign="top">
		<table  width="100%" cellpadding="0" cellspacing="0">
		 	<tr class="headerRow">
				<th width="200" colspan="6">
					<a href="#" onclick="toggleCategoryBalances();"><img src="images/add.png" width="12" border="0"></img></a>
					UNRECONCILED TRANSACTIONS
				</th>
			</tr>
			 	
		 	<tr class="headerRow">
				<th  class="specalt date">date</th>
				<th class="specalt">description</th>
				<th class="specalt rf">Rf</th>
				<th class="specalt expense">Expense</th>
				<th class="specalt income">Income</th>
				<th class="specalt balance">Balance</th>
			</tr>
			<?$result = mysql_query("select * from transaction where accountId = " .$budgetServices->getSavingsAccountId($userid) ." and amount<> 0 and parentTransId=0 and reconciled is false and categoryId<>6 order by date asc");
				while ($row = mysql_fetch_array($result)){
			?>
			<tr id="<?=$row["id"]?>" class="transactions" amount="<?=$row["amount"]?>">
					<td class="transDate"><?=$row["date"]?></td>
					<td class="transDescription" nowrap="true"><?=$row["description"]?></td>
					<td><input class="reconCheckbox" type="checkbox" value="<?=$row["reconciled"]?>" disabled/></td>
					<td class="expenseAmount"><? if ($row["amount"] < 0) echo 'R <label class="transmount">'. $row["amount"] * -1; else echo "&nbsp;" ?></label></td>
					<td class="incomeAmount"><? if ($row["amount"] >= 0) echo 'R <label class="transmount">'. $row["amount"]; else echo "&nbsp;" ?></label></td>
					<td>R 0</td>
				</tr>
			<?
			}
				
			?>
			</table>
		
		</td>
	</tr>
</table>
<br/>


<!-- --********************* HISTORY -->

<table width="700">
		<tr>
		<td style="background: #E6EAE9;" valign="top">
		<table  width="100%" cellpadding="0" cellspacing="0">
		 	<tr class="headerRow">
				<th width="200" colspan="6">
					<a href="#" onclick="toggleCategoryBalances();"><img src="images/add.png" width="12" border="0"></img></a>
						TRANSACTION HISTORY
				</th>
			</tr>
			 	
		 	<tr class="headerRow">
				<th  class="specalt date">date</th>
				<th class="specalt">description</th>
				<th class="specalt rf">Rf</th>
				<th class="specalt expense">Expense</th>
				<th class="specalt income">Income</th>
				<th class="specalt balance">Balance</th>
			</tr>
			<?
				require_once "classes.php";
				$service = new CommonServices;
				$savingsAccId = $budgetServices->getSavingsAccountId($userid);

				$thisMonthStartDate = $service->getThisMonthStartDate($userid);
				$result = mysql_query("select * from transaction where hashkey is not null and accountId=". $savingsAccId." order by date desc limit 10");
			
				while ($row = mysql_fetch_array($result)){
					$amount = $row["amount"];
					if ($amount < 0) $amount = $amount * -1;
			?>
				<tr id="<?=$row["id"]?>">
					<td class="transDate specalt"><?=$row["date"]?></td>
										
					<? if ($row["reconciled"] == 1 ){ ?>
					<td class="transDescription specalt" ><?=$row["description"]?></td>
				    <td class="specalt"><input type="checkbox" value="<?=$row["reconciled"]?>" disabled checked="true"/></td>
				    <? } else { ?>
				    <td class="transDescription specalt" ><a href="#" class="mainTransaction" onclick="getAccountDetails(this); return false;"><?=$row["description"]?></a></td>
				     <td class="specalt"><input type="checkbox" value="<?=$row["reconciled"]?>" disabled/></td>
				     <? } ?>

					<td class="specalt"><? if ($row["amount"] < 0) echo 'R <label class="mainTransAmount">'. $amount; else echo "&nbsp;" ?></label></td>
					<td class="specalt"><? if ($row["amount"] >= 0) echo 'R <label class="mainTransAmount">'. $amount; else echo "&nbsp;" ?></label></td>
					<td class="specalt">R 0</th>
				</tr>
				
			<?
				}
				
			?>
			</table>
		
		</td>
	</tr>
</table>
	
<?php
	mysql_close($con);
	 ?>	
	
<?php include("includes/footer.php"); ?>		
</body>
</html>